package com.luoxiang.socialandroid.fragment;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.RecordofconsumAdapter;
import com.luoxiang.socialandroid.base.LazyLoadFragment;
import com.luoxiang.socialandroid.bean.ConsumeLog;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.bean.RecordofConsumBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Description TODO
 * @systemUser Administrator
 * @Author LX
 * @Date 09-23-2022 周五 10:50
 */
public class RecordsOfConsumptionFragment extends LazyLoadFragment {
    private String token;
    private RecordofconsumAdapter adapter;
    private RecyclerView rv_xiaofeijilu;
    private SmartRefreshLayout smartRefreshLayout;
    private int page = 1;//是我们的页数
    private ArrayList<ConsumeLog> creditLogs;

    @Override
    protected int setContentView() {
        return R.layout.fragment_recordofconsumption;
    }

    @Override
    protected void initView() {

        smartRefreshLayout = findViewById(R.id.smartRefresh);
        rv_xiaofeijilu = findViewById(R.id.rv_xiaofeijilu);
    }


    @Override
    protected void lazyLoad() {

        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        creditLogs = new ArrayList<>();
        getConsumeLog(EXTRA, token, page, 10);
        getRequest();
        smartRefreshLayout.setEnableRefresh(true);
        smartRefreshLayout.setEnableLoadMore(true);
        adapter = new RecordofconsumAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        rv_xiaofeijilu.setAdapter(adapter);
        rv_xiaofeijilu.setLayoutManager(linearLayoutManager);
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getConsumeLog(EXTRA, token, page, 10);
            }
        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getConsumeLog(EXTRA, token, page, 10);

            }
        });
    }

    /**
     * 消费记录列表接口
     *
     * @param extra
     * @param token
     * @param page
     * @param limit
     */
    private void getConsumeLog(String extra, String token, int page, int limit) {
        getRequest();
        NetUtils.getInstance().getApiService().getConsumeLog(extra, token, page, limit)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        clearLoading();
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr7996", decryptStr);
                        Gson gson = new Gson();
                        RecordofConsumBean recordofConsumBean = gson.fromJson(decryptStr, RecordofConsumBean.class);
                        if (creditLogs != null) {
                            if (page == 1) {
                                creditLogs.clear();
                            }
                        }
                        if (recordofConsumBean.getData() != null && recordofConsumBean.getData().getConsumeLog() != null && recordofConsumBean.getData().getConsumeLog().size() > 0) {
                            if (creditLogs != null) {
                                creditLogs.addAll(recordofConsumBean.getData().getConsumeLog());

                            }
                            adapter.setData(creditLogs);
                            adapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.showShort(R.string.mi_wugengduoshuju);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        clearLoading();
                    }

                    @Override
                    public void onComplete() {
                        clearLoading();
                        if (smartRefreshLayout != null) {
                            smartRefreshLayout.finishRefresh();
                            smartRefreshLayout.finishLoadMore();
                        }
                    }
                });
    }
}
